package com.linwen.javase.desgin_pattern.structure.flyweight;

import java.util.HashMap;

/**
 * @ClassName FlyWeightFactory
 * @Description
 * @Author lin
 * @Date 2022/6/22 上午1:27
 * @Version V1.0
 */
public class FlyWeightFactory {
    private HashMap<String, FlyWeight> flyweights = new HashMap<String,FlyWeight>();

    public FlyWeight getFlyWeight(String key){
        if(flyweights.containsKey(key)){
            System.out.println("享元已存在"+flyweights.get(key));
            return flyweights.get(key);
        }
        ConcreteFlyWeight concreteFlyWeight = new ConcreteFlyWeight(key);
        System.out.println("享元第一次创建"+concreteFlyWeight);
        flyweights.put(key,concreteFlyWeight);
        return concreteFlyWeight;
    }

}
